import com.unipay.mybatis.mapper.DynamicSQLMapper;
import com.unipay.mybatis.pojo.Emp;
import com.unipay.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject: SSM
 * @Author: cmy
 * @CreateTime: 2023-01-14  16:19
 * @Description: TODO
 */

public class DynamicSQL {

    @Test
    public void getEmpsByCondition(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
//        Emp emp = new Emp(null, "cmy", 25, "男");
        Emp emp = new Emp(null, null, null, "女");
        List<Emp> empsByCondition = mapper.getEmpsByCondition(emp);
        empsByCondition.forEach(System.out::println);
        sqlSession.close();
    }

    @Test
    public void getEmpsByChoose(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
//        Emp emp = new Emp(null, "cmy", 25, "男");
        Emp emp = new Emp(null, null, null, "女");
        List<Emp> empsByCondition = mapper.getEmpsByChoose(emp);
        empsByCondition.forEach(System.out::println);
        sqlSession.close();
    }


    @Test
    public void insertEmps(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        Emp emp1 = new Emp(null, "Jokic", 26, "男");
        Emp emp2 = new Emp(null, "Micro", 23, "男");
        Emp emp3 = new Emp(null, "Wede", 39, "女");
/*
        List<Emp> emps = new List<Emp>();
        emps.add(emp1);
        emps.add(emp2);
        emps.add(emp3);
*/
        List<Emp> emps = Arrays.asList(emp1, emp2, emp3);
        mapper.insertEmps(emps);
        sqlSession.close();
    }

    @Test
    public void delEmps(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        int delRes = mapper.delEmps(new Integer[]{7, 8, 9});
        System.out.println(delRes);
        sqlSession.close();
    }


    /*
     * @description: 缓存
     * @date: 2023/1/14 19:45
     * @param:
     * @return:
     **/
    @Test
    public void cache1(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        Emp empById = mapper.getEmpById(3);
        System.out.println("0: " + empById);

//        int i = mapper.updateEmp(3);                  // 两次回话之间，有 “增删改” 操作
        sqlSession.clearCache();                        // 清除缓存
        Emp empById1 = mapper.getEmpById(3);
        System.out.println("1: " + empById1);

        SqlSession sqlSession1 = SqlSessionUtil.getSqlSession();
        DynamicSQLMapper mapper1 = sqlSession1.getMapper(DynamicSQLMapper.class);
        Emp empById2 = mapper1.getEmpById(3);
        System.out.println("2: " + empById2);

        sqlSession.close();
        sqlSession1.close();
    }
    @Test
    public void cache2(){
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
            SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
            DynamicSQLMapper mapper1 = sqlSession1.getMapper(DynamicSQLMapper.class);
            Emp emp1 = mapper1.getEmpById(10);
            System.out.println("emp1: " + emp1);
            // 暂时在“一级缓存”；关闭后，才会到“二级缓存”生效
            sqlSession1.close();

            // TODO 两次查询之间，进行任意的“增删改”，同样会使 二级缓存 失效

            SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
            DynamicSQLMapper mapper2 = sqlSession2.getMapper(DynamicSQLMapper.class);
            Emp emp2 = mapper2.getEmpById(10);
            System.out.println("emp2: " + emp2);
            sqlSession2.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
